{
 "cells": [
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## **演示0401：导数的概念**"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### **导数定义**\n",
    "* 设函数$y=f(x)$在点$x_0$的某个邻域内有定义，当自变量$x$在$x_0$处有增量$\\Delta x$，且$(x_0+\\Delta x)$也在该邻域内时，相应的函数取得增量：$ \\Delta y=f(x_0+\\Delta x)-f(x_0) $  \n",
    "![](images/040101.png)  \n",
    "* 如果当$ \\Delta x \\to 0 $时，$ \\frac{\\Delta y}{\\Delta x} $之比的极限存在，则称函数$y=f(x)$在点$x_0$处可导，并且该极限值称为函数$y=f(x)$在点$x_0$处的导数，可记作：$f'(x_0)$，或者：$y'|x=x_0$，或者：$\\dfrac{dy}{dx}|x=x_0$\n",
    "* 导数的数值计算方法：$f'(x_0)=\\lim_{\\Delta x \\to 0} \\dfrac{\\Delta y}{\\Delta x}=\\lim_{\\Delta x \\to 0} {\\dfrac{f(x_0 + \\Delta x)-f(x_0)}{\\Delta x}} $，有时候也用：$f'(x_0)=\\lim_{\\Delta x \\to 0} {\\dfrac{f(x_0 + \\Delta x)-f(x_0 - \\Delta x)}{2\\Delta x}} $\n",
    "* 函数$y=f(x)$在点$x_0$的导数，就是函数$f(x)$在点$(x_0, f(x_0))$处的切线的斜率"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### **案例1：观察导数和斜率**\n",
    ">  \n",
    "* 做出函数$y=x^2$的曲线图\n",
    "* 计算该函数在x=1.5处的导数(切线斜率)\n",
    "* 绘制通过该点的切线"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "image/png": "iVBORw0KGgoAAAANSUhEUgAAAW4AAAD8CAYAAABXe05zAAAABHNCSVQICAgIfAhkiAAAAAlwSFlzAAALEgAACxIB0t1+/AAAADl0RVh0U29mdHdhcmUAbWF0cGxvdGxpYiB2ZXJzaW9uIDIuM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r3Qnzv6y7KaWsdhrLsIcFa62vsnDTr4AfgZesGKfGLrYfSqnbgeHAIG2PFxTOcAk/E0eTAYSd8X5T4IhBWcQZqs8HfwfM1Vp/b3Se2qC1PqWUWgMMAaxyAdkuT5UopaLPeHckkGBUlsuhlBoCPAWM1FoXGZ2nDtsGRCulIpVS7sAYYLHBmeq86gt6M4H9WuspRue5HEqpoD+mxpRSXsBVWLG37HWq5DugFeYphjTgPq11prGpLp1SKhnwAI5Xf2izI07HACilrgc+AIKAU8BOrfVgY1NZTik1FJgKmIDPtNb/MjhSjSmlvgb6Y16J7hjwktZ6pqGhakAp1QdYD+zB/P86wLNa62XGpaoZpVQHYDbm3y8XYL7W+hWrvZ49FrcQQojzs8tTJUIIIc5PilsIIRyMFLcQQjgYKW4hhHAwUtxCCOFgpLiFEMLBSHELIYSDkeIWQggH8/9z/VkmqZkBXQAAAABJRU5ErkJggg==\n",
      "text/plain": [
       "<matplotlib.figure.Figure at 0x2a6ecb4d550>"
      ]
     },
     "metadata": {},
     "output_type": "display_data"
    }
   ],
   "source": [
    "''' 使用导数的定义计算某个点的导数值，并绘制该点的切线 '''\n",
    "\n",
    "%matplotlib inline\n",
    "import numpy as np\n",
    "import matplotlib.pyplot as plt\n",
    "\n",
    "def fun(x):\n",
    "    return x*x\n",
    "\n",
    "x = np.linspace(-3, 3, 50)\n",
    "y = fun(x)\n",
    "\n",
    "x0 = 1.5\n",
    "y0 = fun(x0)\n",
    "\n",
    "delta_x = 1e-4\n",
    "x1 = x0 + delta_x\n",
    "y1 = fun(x1)\n",
    "delta_y = y1 - y0\n",
    "\n",
    "coef= delta_y / delta_x    # 斜率\n",
    "intecept = y0 - coef*x0    # 截距\n",
    "\n",
    "plt.plot(x, y)\n",
    "# 绘制穿过x0点的斜率值线\n",
    "x_left = x0 - 1\n",
    "y_left = coef*x_left + intecept\n",
    "x_right = x0 + 1\n",
    "y_right = coef*x_right + intecept\n",
    "plt.plot([x_left,x_right], [y_left, y_right], 'b')\n",
    "\n",
    "plt.scatter(x0, fun(x0), color='r')\n",
    "plt.show()"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### **使用分析法求导数**\n",
    "* 在很多情况下，用前述数值方法计算导数会导致较大的计算量。因此，也常用分析法来计算导数。在分析法中，首先需要获得函数$f(x)$的导函数\n",
    "* 如果函数$y=f(x)$在开区间内每一点都可导，就称函数在$f(x)$在该区间内可导。此时$f(x)$对于在区间内的每一个确定的$x$值，都对应着一个确定的导数值，从而构成一个新函数，即原来函数$y=f(x)$的导函数，记作：$y', f'(x), \\dfrac{dy}{dx} $ 或者$\\dfrac{df(x)}{dx}$。导函数也简称导数\n",
    "* 常见函数的导函数：\n",
    " * $y=C$ ($C$为常数): $y'=0$\n",
    " * $y=x^n$ ($n$为常数)：$y'=n \\cdot x^{n-1}$\n",
    " * $y=a^x$ ($a$为常数)：$y'=a^x \\cdot \\ln a$\n",
    " * $y=\\log_a^x$ ($a$为常数)：$y'=\\dfrac{1}{x \\ln a}$\n",
    " * $y=sinx$：$y'=cosx$\n",
    " * $y=cosx$：$y'=-sinx$\n",
    " * $y=tanx$：$y'=sec^2 x$\n",
    " * $y=cotx$：$y'=-csc^2 x$\n",
    "* 导数的复合运算：\n",
    " * $(Cu)'=Cu'$ ($C$为常数)\n",
    " * $(u \\pm v)'=u' \\pm v' $\n",
    " * $(uv)'= u'v + uv' $\n",
    " * $(u/v)'=\\dfrac{u'v - uv'}{v^2} $"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### **复杂函数的链式求导**\n",
    "* 链式求导法则：复合函数$F(x)$对自变量$x$的导数，等于该函数对中间变量$t$的导数$F'(t)$，乘以中间变量对自变量的导数$T'(x)$\n",
    "* 示例1：计算函数$F(x)=(3x^2-2x+1)^2 $的导数\n",
    " * 设：$ t=3x^2-2x+1 $\n",
    " * 则：$ F(t)=t^2, t(x)=3x^2-2x+1 $\n",
    " * 因此：$ F'(x)=F'(t) \\cdot t'(x)=(2 \\cdot t) \\cdot (3 \\cdot 2 \\cdot x-2+0)=2 \\cdot (3x^2-2x+1) \\cdot (6x -2) $\n",
    "* 示例2：计算函数$F(x)=\\dfrac{1}{1+e^{-x}}$的导数\n",
    " * 设：$ t=1+e^{-x} $\n",
    " * 则：$ F(t)=t^{-1},t(x)=1+e^{-x} $\n",
    " * 因此：  \n",
    "$ \\begin{aligned}\n",
    "F'(x)&=F'(t) \\cdot t'(x)=-t^{-2} \\cdot (e^{-x} \\cdot (-1))\\\\ \\\\\n",
    "&=\\dfrac{1}{1+e^{-x}} \\cdot \\dfrac{e^{-x}}{1+e^{-x}}=\\dfrac{1}{1+e^{-x}} \\cdot (1-\\dfrac{1}{1+e^{-x}}) \\\\ \\\\\n",
    "&=F(x) \\cdot (1-F(x))\n",
    "\\end{aligned} $"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### **案例2：以数值法和分析法分别计算导数**\n",
    "* 计算函数$F(x)=\\dfrac{1}{1+e^{-x}}$在$x=-1.0, -0.5, -0.25, 0, 0.25, 0.5, 1.0$时的导数\n",
    "* *scipy.misc.derivative*可直接用于求导数"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 3,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "[0.19661198 0.22415743 0.24318218 0.25       0.24318214 0.22415735\n",
      " 0.19661189]\n",
      "[0.19661193 0.22415739 0.24318216 0.25       0.24318216 0.22415739\n",
      " 0.19661193]\n",
      "[0.19661193 0.22415739 0.24318216 0.25       0.24318216 0.22415739\n",
      " 0.19661193]\n"
     ]
    }
   ],
   "source": [
    "''' 分别以数值发和分析法计算导数值并比较结果 '''\n",
    "import numpy as np\n",
    "from scipy.misc import derivative\n",
    "\n",
    "def f(x):\n",
    "    return 1.0 / (1.0 + np.exp(-x))\n",
    "\n",
    "x = np.linspace(-1.0, 1.0, 7)\n",
    "\n",
    "# 数值方法求导数\n",
    "delta_x = 1e-6\n",
    "dir_numerical = (f(x + delta_x) - f(x)) / delta_x\n",
    "\n",
    "# 分析方法求导数\n",
    "def dir_f(x):\n",
    "    return f(x)*(1-f(x))\n",
    "\n",
    "dir_analytical = dir_f(x)\n",
    "\n",
    "# 采用scipy求导数\n",
    "dir_scipy = derivative(f, x, dx=1e-6)    # dx用于指定导数计算时的x微小偏移量\n",
    "\n",
    "# 对比对应元素的值，其差距应该非常小\n",
    "print(dir_numerical)\n",
    "print(dir_analytical)\n",
    "print(dir_scipy)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### **偏导数**\n",
    "* 针对一个变量求偏导时，可以把其它变量视为常数\n",
    "* 示例1：求函数$ f(x,y)=x^2+2xy+y^2 $ 的偏导数\n",
    " * 需要针对自变量$x$和$y$分别来求。\n",
    " * 也可以看成在$xoy$平面内，分别求沿$x$方向的导数和$y$方向的导数。\n",
    " * $ \\dfrac{\\partial f}{\\partial x}= \\dfrac{\\partial(x^2+2xy+y^2)}{\\partial x}= \\dfrac{\\partial{x^2}+\\partial{(2xy)}+\\partial{y^2}}{\\partial x}=2x+2y+0=2x+2y $\n",
    " * $ \\dfrac{\\partial f}{\\partial y}= \\dfrac{\\partial(x^2+2xy+y^2)}{\\partial y}= \\dfrac{\\partial{x^2}+\\partial{(2xy)}+\\partial{y^2}}{\\partial y}=0+2x+2y=2x+2y $"
   ]
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.8.8"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 4
}
